#!/bin/bash


IFS=$'\n'

for p in $(cat basic.chain | grep '^[^#]'); do
	echo "create new make file for $p";
	
	SRC=basic/old/make_$p*.sh
	if [ ! -r $SRC ]; then
		echo "Can not find old make script... $SRC";
		
		printf "" > basic/make_$p.sh_;
        filelines=`cat ./basic/make_SKEL.sh`
        for l in $filelines; do
            if [[ $l == "# BLK"? ]]; then
                echo "" >> basic/make_$p.sh_;
                echo "" >> basic/make_$p.sh_;
                echo "" >> basic/make_$p.sh_;
                echo "" >> basic/make_$p.sh_;
                echo "" >> basic/make_$p.sh_;
            else
                echo "$l" >> basic/make_$p.sh_;
            fi
        done

	else
		
		FLEN=$(cat $SRC | wc -l);
		
		# copy prepare source block
		BLKBEGIN=$(cat $SRC | grep -n '^# package source modifications' | grep '[0-9]:.*' | cut -d: -f1);
		if [[ $BLKBEGIN == "" ]]; then
			echo "Can not find package source modifications block...";
			exit 1;
		fi
		BLKEND=$(cat $SRC | tail -$((FLEN-BLKBEGIN)) | grep -n -m 1 '^##############################' | grep '[0-9]:.*' | cut -d: -f1);
		if [[ $BLKEND == "" ]]; then
			echo "Can not find end of package source modifications block...";
			exit 2;
		fi
		echo "Block detected from line: $BLKBEGIN with length: $BLKEND";
		NEWBLK1="$(cat $SRC | tail -$((FLEN-BLKBEGIN)) | head -$((BLKEND-1)) | sed -e 's/$PKG_FOLDER/$PKG_DEST/g')";
		
		# copy make block
		BLKBEGIN=$(cat $SRC | grep -n '^# configure and make' | grep '[0-9]:.*' | cut -d: -f1);
		if [[ $BLKBEGIN == "" ]]; then
			echo "Can not find package source modifications block...";
			exit 1;
		fi
		BLKEND=$(cat $SRC | tail -$((FLEN-BLKBEGIN)) | grep -n -m 1 '^##############################' | grep '[0-9]:.*' | cut -d: -f1);
		if [[ $BLKEND == "" ]]; then
			echo "Can not find end of package source modifications block...";
			exit 2;
		fi
		echo "Block detected from line: $BLKBEGIN with length: $BLKEND";
		NEWBLK2="$(cat $SRC | tail -$((FLEN-BLKBEGIN)) | head -$((BLKEND-1)) | sed -e 's/$PKG_FOLDER/$PKG_DEST/g')";
		
		# copy package modification block
		BLKBEGIN=$(cat $SRC | grep -n '^# package modifications' | grep '[0-9]:.*' | cut -d: -f1);
		if [[ $BLKBEGIN == "" ]]; then
			echo "Can not find package source modifications block...";
			exit 1;
		fi
		BLKEND=$(cat $SRC | tail -$((FLEN-BLKBEGIN)) | grep -n -m 1 '^##############################' | grep '[0-9]:.*' | cut -d: -f1);
		if [[ $BLKEND == "" ]]; then
			echo "Can not find end of package source modifications block...";
			exit 2;
		fi
		echo "Block detected from line: $BLKBEGIN with length: $BLKEND";
		NEWBLK3="$(cat $SRC | tail -$((FLEN-BLKBEGIN)) | head -$((BLKEND-1)) | sed -e 's/$PKG_FOLDER/$PKG_DEST/g')";
		
		
		printf "" > basic/make_$p.sh;
		filelines=`cat ./basic/make_SKEL.sh`
		for l in $filelines; do
			if [[ $l == "# BLK1" ]]; then
				echo "$NEWBLK1" >> basic/make_$p.sh;
				echo "" >> basic/make_$p.sh;
			elif [[ $l == "# BLK2" ]]; then
				echo "$NEWBLK2" >> basic/make_$p.sh;
				echo "" >> basic/make_$p.sh;
			elif [[ $l == "# BLK3" ]]; then
				echo "$NEWBLK3" >> basic/make_$p.sh;
				echo "" >> basic/make_$p.sh;
			else
				echo "$l" >> basic/make_$p.sh;
			fi
		done
		
	fi
done
